unit uSelectCust;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxContainer, cxEdit, dxSkinsCore, dxSkinBlack,
  dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue, cxGroupBox, cxTextEdit, Vcl.Menus, Vcl.StdCtrls, cxButtons,
  cxStyles, dxSkinscxPCPainter, cxCustomData, cxFilter, cxData, cxDataStorage,
  cxNavigator, Data.DB, cxDBData, cxGridLevel, cxClasses, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  Datasnap.DBClient, ClientRowSet;

type
  TFrmSelectCust = class(TForm)
    cxGroupBox1: TcxGroupBox;
    edtCust: TcxTextEdit;
    cxButton2: TcxButton;
    Grdcust: TcxGrid;
    tvCust: TcxGridDBTableView;
    lvEXP_COMP: TcxGridLevel;
    crsCust: TClientRowSet;
    crsCustCOMP_ID: TStringField;
    crsCustXUNY_NO: TStringField;
    crsCustCOMP_ABB: TStringField;
    crsCustCOMP_NAME: TStringField;
    crsCustINDUSTRY: TStringField;
    crsCustCITY: TStringField;
    crsCustADDRESS: TStringField;
    crsCustTEL: TStringField;
    crsCustFEX: TStringField;
    crsCustWEB_ADDR: TStringField;
    crsCustEMAIL: TStringField;
    crsCustBUSI_SCOPE: TStringField;
    crsCustSRV_AREA: TStringField;
    crsCustDEFAULT_CONTACTOR: TStringField;
    crsCustCARD_NO: TStringField;
    crsCustDOWN_TIME: TStringField;
    crsCustUPDATETIME: TStringField;
    crsCustSYS_ACCOUNT: TStringField;
    crsCustVERSION: TSmallintField;
    crsCustPOST_CODE: TStringField;
    crsCustBANK: TStringField;
    crsCustBANK_ADDR: TStringField;
    crsCustBANK_CARDNO: TStringField;
    crsCustTEAM: TStringField;
    crsCustCUST_SCALE: TStringField;
    crsCustCUST_TYPE: TStringField;
    crsCustGROUP_CODE: TStringField;
    crsCustGROUP_NAME: TStringField;
    crsCustCUST_GROUP: TStringField;
    crsCustCUST_NODE: TStringField;
    crsCustMANAGER_TEL: TStringField;
    crsCustNODE_NO: TStringField;
    crsCustNODE_NAME: TStringField;
    crsCustCHECKED: TStringField;
    crsCustPAYSIDE: TIntegerField;
    crsCustFETCH_EMP_NAME: TStringField;
    dsCust: TDataSource;
    btnConfirm: TcxButton;
    tvCustCOMP_NAME: TcxGridDBColumn;
    tvCustTEL: TcxGridDBColumn;
    tvCustNODE_NAME: TcxGridDBColumn;
    tvCustCARD_NO: TcxGridDBColumn;
    Label1: TLabel;
    crsCustRISK_STATUS: TSmallintField;
    tvCustColumn1: TcxGridDBColumn;
    tvCustXUNY_NO: TcxGridDBColumn;
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure cxButton2Click(Sender: TObject);
    procedure btnConfirmClick(Sender: TObject);
    procedure tvCustDblClick(Sender: TObject);
    procedure tvCustEditDblClick(Sender: TcxCustomGridTableView;
      AItem: TcxCustomGridTableItem; AEdit: TcxCustomEdit);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure SearchData;
  public
    { Public declarations }
  end;

var
  FrmSelectCust: TFrmSelectCust;
  function SelectCustShow: string;
  procedure SelectCustShowEx(var custNo, custName, custCont, custTel, custAddr,node,nodename: string; var risk_status:Integer);

implementation
uses Console, uDm, ucommFunction;
{$R *.dfm}

procedure TFrmSelectCust.btnConfirmClick(Sender: TObject);
begin
  if (not crsCust.Active) or (crsCust.RecordCount = 0) then
    Exit;
  ModalResult := mrOk;
end;

procedure TFrmSelectCust.cxButton2Click(Sender: TObject);
begin
  SearchData;
end;

procedure TFrmSelectCust.FormCreate(Sender: TObject);
begin
  pSetCaption(Self);
end;

procedure TFrmSelectCust.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    SelectNext(ActiveControl, True, True);
end;

procedure TFrmSelectCust.FormShow(Sender: TObject);
begin
  SearchData;
end;

procedure TFrmSelectCust.SearchData;
var
  SQLText: string;
begin
  SQLText := 'SELECT ' + GetClientRowSetColumns(crsCust) + ' FROM bas_company WHERE group_code=' + QuotedStr(UserInfo.GroupCode);
  if not UserInfo.IsCenter then
    SQLText := SQLText + ' AND node_no=' + QuotedStr(UserInfo.NodeNo);
  if Trim(edtCust.Text) <> '' then
    SQLText := SQLText + ' AND (card_no LIKE' + QuotedStr(edtCust.Text + '%') +
      ' OR tel=' + QuotedStr(edtCust.Text) +' OR comp_name LIKE ' + QuotedStr('%' + edtCust.Text + '%') + ')';
  crsCust.Close;
  crsCust.CommandText := SQLText;
  crsCust.Open;
end;

procedure TFrmSelectCust.tvCustDblClick(Sender: TObject);
begin
  btnConfirm.Click;
end;

procedure TFrmSelectCust.tvCustEditDblClick(Sender: TcxCustomGridTableView;
  AItem: TcxCustomGridTableItem; AEdit: TcxCustomEdit);
begin
  btnConfirm.Click;
end;

function SelectCustShow: string;
begin
  Result := '';
  Application.CreateForm(TFrmSelectCust, FrmSelectCust);
  with FrmSelectCust do
  try
    if ShowModal = mrOK then
      Result := crsCust.FieldByName('card_no').AsString;
  finally
    FreeAndNil(FrmSelectCust);
  end;
end;

procedure SelectCustShowEx(var custNo, custName, custCont, custTel, custAddr, node, nodename: string; var risk_status:Integer);
begin
  custNo := '';
  custName := '';
  custCont := '';
  custTel := '';
  custAddr := '';
  node:='';
  nodename:='';
  risk_status:=0;
  Application.CreateForm(TFrmSelectCust, FrmSelectCust);
  with FrmSelectCust do
  try
    if ShowModal = mrOK then
    begin
      custNo := crsCust.FieldByName('card_no').AsString;
      custName := crsCust.FieldByName('comp_name').AsString;
      custCont := crsCust.FieldByName('default_contactor').AsString;
      custTel := crsCust.FieldByName('tel').AsString;
      custAddr := crsCust.FieldByName('address').AsString;
      node := crsCust.FieldByName('node_no').AsString;
      nodename := crsCust.FieldByName('node_name').AsString;
      risk_status:=crsCust.FieldByName('RISK_STATUS').AsInteger;
    end;
  finally
    FreeAndNil(FrmSelectCust);
  end;
end;


end.
